package com.huaxin.hxcloud.work;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.RepairListDTO;
import com.huaxin.common.Entity.RepairRecord;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.hxcloud.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/29.
 * 首创维修记录
 */

public class HxScRepairRecordActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.more)
    TextView           mMore;
    @BindView(R.id.toolbar)
    Toolbar            mToolbar;
    @BindView(R.id.recyclerview)
    RecyclerView       mRecyclerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.content)
    FrameLayout        mContent;
    private String mDeviceCode;
    private              int mPage        = 0;
    private final static int TYPE_REFRESH = 1;
    private final static int TYPE_MORE    = 2;
    private              int mPageSize    = 10;
    private HxScRepairRecordAdapter mRecordAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_sc_repair_record;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle("维修记录");
        mRecordAdapter = new HxScRepairRecordAdapter(HxScRepairRecordActivity.this);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mSwipeLayout.setOnRefreshListener(this);
        mRecordAdapter.setOnLoadMoreListener(this, mRecyclerview);
        mRecyclerview.setAdapter(mRecordAdapter);
    }


    @Override
    public void requestData() {
        updateLoadStatus(LOADING);
        ApiRequestData(TYPE_REFRESH, mPage);
    }

    private void ApiRequestData(final int refreshState, int pageNum) {
        Call<RepairListDTO> call = HxTransmitRequestHelper.getRequestApi().getScRepairRecord(mDeviceCode, pageNum, mPageSize);
        autoClose(call);
        call.enqueue(new Callback<RepairListDTO>() {
            @Override
            public void onResponse(Call<RepairListDTO> call, Response<RepairListDTO> response) {
                RepairListDTO repairListDTO = response.body();
                List<RepairRecord> workTaskRecord = repairListDTO.getWorkTaskRecord();
                if (repairListDTO.isSuccessful()) {
                    if (workTaskRecord == null || workTaskRecord.size() == 0) {
                        updateLoadStatus(LOAD_NO_DATA);
                        return;
                    }
                    updateLoadStatus(LOAD_SUCCESS);
                    switch (refreshState) {
                        case TYPE_REFRESH://下拉刷新
                            if (workTaskRecord.size() == 0) {
                                mRecordAdapter.getData().clear();
                            } else {
                                mRecordAdapter.setNewData(workTaskRecord);
                            }
                            mSwipeLayout.setRefreshing(false);
                            mRecordAdapter.setEnableLoadMore(true);
                            mRecordAdapter.loadMoreComplete();
                            break;
                        case TYPE_MORE://上拉加载更多
                            if (workTaskRecord.size() == 0) {
                                mRecordAdapter.loadMoreEnd(false);
                            } else {
                                mRecordAdapter.setEnableLoadMore(true);
                                mRecordAdapter.addData(workTaskRecord);
                                mRecordAdapter.loadMoreComplete();
                            }
                            mSwipeLayout.setEnabled(true);
                            break;
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }

            }

            @Override
            public void onFailure(Call<RepairListDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    public static void start(Context context, String deviceCode) {
        Intent intent = new Intent(context, HxScRepairRecordActivity.class);
        intent.putExtra(Constants.IK_DEVICE_CODE, deviceCode);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras.containsKey(Constants.IK_DEVICE_CODE)) {
            mDeviceCode = extras.getString(Constants.IK_DEVICE_CODE);
        } else {
            finish();
        }
    }


    @Override
    public void onRefresh() {
        mPage = 0;
        ApiRequestData(TYPE_REFRESH, mPage);
    }

    @Override
    public void onLoadMoreRequested() {
        //少于一页的数量
        if (mRecordAdapter.getData().size() < 10) {
            mRecordAdapter.loadMoreEnd(true);
        } else {
            mPage++;
            //加载更多操作,下拉暂时不可用
            mSwipeLayout.setEnabled(false);
            ApiRequestData(TYPE_MORE, mPage);
            Log.d(TAG, "onRefresh: ---加载更多->");
        }
    }
}